package com.tim.javaApi.reflect.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyFactory<T> implements InvocationHandler {
    //pass为被代理的类
    private T pass;

    public ProxyFactory(T pass){
        this.pass=pass;
    }

    public T getInstance(){
        T proxy = (T) Proxy.newProxyInstance(pass.getClass().getClassLoader(), pass.getClass().getInterfaces(), this);
        return proxy;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("执行方法前");
        //这里会输出UserServiceImpl里对应的方法,不过这里只有个add
        //也就是会输出 “执行了增加的方法”
        Object invoke = method.invoke(pass, args);
        System.out.println("执行方法后");
        return invoke;
    }
}
